package com.pkutech.carcasher.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.TextView;

import com.dd.processbutton.iml.ActionProcessButton;
import com.pkutech.carcasher.R;
import com.pkutech.carcasher.bean.BaseResponse;
import com.pkutech.carcasher.model.SelectBookingOrder;
import com.pkutech.carcasher.network.SoapApi;
import com.pkutech.carcasher.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by cyy on 2016/6/28.
 */
public class OrderDetailActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.confirmBtn)
    ActionProcessButton confirmBtn;
    @BindView(R.id.backBtn)
    ActionProcessButton backBtn;

    SelectBookingOrder selectBookingOrder;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.text5)
    TextView text5;
    @BindView(R.id.text6)
    TextView text6;
    @BindView(R.id.text7)
    TextView text7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        initToolbar(toolbar);

        selectBookingOrder = (SelectBookingOrder) getIntent().getSerializableExtra(OrderQueryActivity.TO_ORDER);

        text1.setText(selectBookingOrder.getRoadid());
        text2.setText(selectBookingOrder.getCarportid());
        text3.setText(selectBookingOrder.getCarnum());
        text4.setText(selectBookingOrder.getBookingtime());
        text5.setText(selectBookingOrder.getCreatedate());
        if(!TextUtils.isEmpty(selectBookingOrder.getState()) && "2".equals(selectBookingOrder.getState())){
            text6.setText("订单待审核");
        }else {
            text6.setText("订单无效");
        }
        text7.setText(selectBookingOrder.getCreateuser());
    }

    @OnClick(R.id.confirmBtn)
    public void confirm() {
        SoapApi.confirm(selectBookingOrder.getCode())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        confirmBtn.setProgress(1);
                    }
                })
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {
                        confirmBtn.setProgress(0);
                    }

                    @Override
                    public void onError(Throwable e) {
                        confirmBtn.setProgress(0);
                        ToastUtil.showInfoToast(OrderDetailActivity.this, "确认失败", ToastUtil.Position.TOP);
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        confirmBtn.setProgress(0);
                        if (baseResponse != null && !TextUtils.isEmpty(baseResponse.getStatus()) && "true".equals(baseResponse.getStatus())) {
                            ToastUtil.showSuccessToast(OrderDetailActivity.this, "确认成功", ToastUtil.Position.TOP);
                            setResult(Activity.RESULT_OK);
                            finish();
                        } else {
                            if (baseResponse != null && !TextUtils.isEmpty(baseResponse.getMessage())) {
                                ToastUtil.showInfoToast(OrderDetailActivity.this, baseResponse.getMessage(), ToastUtil.Position.TOP);
                            } else {
                                ToastUtil.showInfoToast(OrderDetailActivity.this, "确认失败", ToastUtil.Position.TOP);
                            }
                        }
                    }
                });
    }

    @OnClick(R.id.backBtn)
    public void back() {
        SoapApi.back(selectBookingOrder.getCode())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        backBtn.setProgress(1);
                    }
                })
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {
                        backBtn.setProgress(0);
                    }

                    @Override
                    public void onError(Throwable e) {
                        backBtn.setProgress(0);
                        ToastUtil.showInfoToast(OrderDetailActivity.this, "驳回失败", ToastUtil.Position.TOP);
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        backBtn.setProgress(0);
                        ToastUtil.showSuccessToast(OrderDetailActivity.this, "驳回成功", ToastUtil.Position.TOP);
                        setResult(Activity.RESULT_OK);
                        finish();
                    }
                });
    }

}
